﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RzCorportativo.Views
{
    public partial class Login : System.Web.UI.UserControl
    {
        public Lib.Login Model
        {
            get
            {
                Lib.Login login = new Lib.Login();
                login.LoginText = this.Email.Text;
                login.Senha = EFramework.Web.Security.RijndaelSimple.Encrypt(this.Senha.Text);
                login.LoginId = EFramework.Web.TypeExtension.IntExtension.Parse(this.LoginId.Value, this.LoginId.Value.Length > 0 ? -1 : 0);
                return login;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
                return;
            //this.Grupo.Attributes.Add("url", this.ResolveUrl("~/Views/Administrativo/Coordenador.aspx/ListarGrupo"));
        }

        public List<string> GetErros()
        {
            List<string> erros = new List<string>();
            if (this.Model.LoginId == -1) 
                erros.Add("Login inválido");
            if (this.Email.Text.Length == 0 || !EFramework.Web.Validation.ServiceValidation.EmailIsValid(this.Email.Text, false)) 
                erros.Add("E-mail inválido");
            if (this.Senha.Enabled && this.ConfirmarSenha.Enabled)
            {
                if (this.Senha.Text.Length == 0)
                    erros.Add("Senha inválida");
                if (!this.Senha.Text.Equals(this.ConfirmarSenha.Text))
                    erros.Add("Confirmação de senha inválida");
            }
            return erros;
        }

        internal void LoadModel(Lib.Login login, bool enable)
        {
            if (!enable)
            {
                Email.Enabled = this.Senha.Enabled = this.ConfirmarSenha.Enabled = enable;
            }
            if (login == null)
                return;
            this.Email.Text = login.LoginText;
            this.LoginId.Value = login.LoginId.ToString();
            this.Senha.Text = login.Senha;
            this.ConfirmarSenha.Text = login.Senha;
        }
    }
}